import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import numeral from 'numeral';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import * as menuService from '../../../services/menus'
import * as com from '../../../utils/common';
import INDI_QTZB_DIVI_FHQK from './INDI_QTZB_DIVI_FHQK'
/**信用卡 */
const daoName = "CM_CRDT_CARD_SUM_M";
const ButtonGroup = Button.Group;
const renderNumber = (num) => {
    let value = ''
    if (num) {
        value = numeral(num).format('0,0')
    }
    return value
};
const columns = [
    { title: '数据日期', width: 140, sorter: true, caption: "Sjrq", key: 'Sjrq', render: data => datetime.format(data.Sjrq, "yyyy-MM-dd"), dispaly: false, filter: true },
    { title: '客户名称', dataIndex: 'Khmc', key: 'Khmc', width: 120, sorter: true, dispaly: true, filter: true },
    { title: '信用额度', className: 'column-money', render: data => com.formatNumber(data.Xyed), caption: "Xyed", key: 'Xyed', width: 120, sorter: true, dispaly: true, filter: true },
    { title: '可用额度', className: 'column-money', render: data => com.formatNumber(data.Kyed), caption: "Kyed", key: 'Kyed', width: 120, sorter: true, dispaly: true, filter: true },
    { title: '激活标志', dataIndex: 'Jhbz', key: 'Jhbz', width: 140, sorter: true, dispaly: true, filter: true },
    { title: '证件号码', dataIndex: 'Zjhm', key: 'Zjhm', width: 170, sorter: true, dispaly: true, filter: true },
    { title: '卡号', dataIndex: 'Khao', key: 'Khao', width: 140, sorter: true, dispaly: true, filter: true },
    { title: '发卡日期', width: 110, sorter: true, caption: "Fkrq", key: 'Fkrq', render: data => datetime.format(data.Fkrq, "yyyy-MM-dd"), dispaly: true, filter: true },
    { title: '最近激活日期', width: 130, sorter: true, caption: "Zjjhrq", key: 'Zjjhrq', render: data => datetime.format(data.Zjjhrq, "yyyy-MM-dd"), dispaly: true, filter: true },
    { title: '销卡日期', width: 110, sorter: true, caption: "Xkrq", key: 'Xkrq', render: data => datetime.format(data.Xkrq, "yyyy-MM-dd"), dispaly: true, filter: true },
    { title: '核准日期', width: 110, sorter: true, caption: "Hzrq", key: 'Hzrq', render: data => datetime.format(data.Hzrq, "yyyy-MM-dd"), dispaly: true, filter: true },
    { title: '客户经理1', width: 100, sorter: false, dataIndex: 'YG1', key: 'YG1', dispaly: true, filter: false },
    { title: '分配比例1(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB1', render: data => renderNumber(data.ZB1), key: 'ZB1', dispaly: true, filter: false },
    { title: '客户经理2', width: 100, sorter: false, dataIndex: 'YG2', key: 'YG2', dispaly: true, filter: false },
    { title: '分配比例2(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB2', render: data => renderNumber(data.ZB2), key: 'ZB2', dispaly: true, filter: false },
    { title: '客户经理3', width: 100, sorter: false, dataIndex: 'YG3', key: 'YG3', dispaly: true, filter: false },
    { title: '分配比例3(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB3', render: data => renderNumber(data.ZB3), key: 'ZB3', dispaly: true, filter: false },
    { title: '客户经理4', width: 100, sorter: false, dataIndex: 'YG4', key: 'YG4', dispaly: true, filter: false },
    { title: '分配比例4(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB4', render: data => renderNumber(data.ZB4), key: 'ZB4', dispaly: true, filter: false },
    { title: '客户经理5', width: 100, sorter: false, dataIndex: 'YG5', key: 'YG5', dispaly: true, filter: false },
    { title: '分配比例5(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB5', render: data => renderNumber(data.ZB5), key: 'ZB5', dispaly: true, filter: false },
];
const controls = [
    { name: 'Sjrq', required: true, type: 'date', label: '数据日期', grid: 12 / 24 },
    { name: 'Khmc', required: true, type: 'text', label: '客户名称', grid: 12 / 24 },
    { name: 'Xyed', required: true, type: 'alphanum', label: '信用额度', grid: 12 / 24 },
    { name: 'Kyed', required: true, type: 'alphanum', label: '可用额度', grid: 12 / 24 },
    { name: 'Jhbz', required: true, type: 'text', label: '激活标志', grid: 12 / 24 },
    { name: 'Zjhm', required: true, type: 'text', label: '证件号码', grid: 12 / 24 },
    { name: 'Khao', required: true, type: 'text', label: '卡号', grid: 12 / 24 },
    { name: 'Fkrq', required: true, type: 'date', label: '发卡日期', grid: 12 / 24 },
    { name: 'Zjjhrq', required: true, type: 'date', label: '最近激活日期', grid: 12 / 24 },
    { name: 'Xkrq', required: true, type: 'date', label: '销卡日期', grid: 12 / 24 },
    { name: 'Hzrq', required: true, type: 'date', label: '核准日期', grid: 12 / 24 },
]
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
class CM_CRDT_CARD_SUM_M extends Component {
    constructor(props) {
        super(props);
        this.state = {
            dataChange: false
        };
    }
    componentDidMount() {
        this.props.dispatch({
            type: `${daoName}/setKeyName`,
            payload: { keyName: "ID", permID: this.props.permID }
        });
        const form = this.refs.findForm.getForm();
        let nyc = '';
        let nym = '';
        form.validateFields((err, values) => {
            if (!err) {
                if (values.YF === undefined || values.YF === '' || values.YF === null) {
                    message.info('请选择年月！');
                } else {
                    let date = new Date(values.YF.get('year'), values.YF.get('month'), 1, 8, 0, 0);
                    let year = date.getFullYear();
                    let month = date.getMonth() + 1;
                    let day = new Date(year, month, 0);
                    if (month < 10) {
                        month = '0' + month;
                    }
                    nym = year + '-' + month + '-' + day.getDate();
                    nyc = datetime.format(date, "yyyy-MM-dd");
                }
            }
        });
        let whereItems = [];
        let WhereCauses = [];
        whereItems.push({
            FieldName: "Sjrq",
            Operator: ">=",
            FieldValues: nyc
        }, {
                FieldName: "Sjrq",
                Operator: "<=",
                FieldValues: nym
            });
        WhereCauses.push({
            WhereItems: whereItems,
            RelationSign: "AND"
        })
        let seeks = WhereCauses;
        this.props.dispatch({
            type: `${daoName}/fetch`,
            payload: { daoName, page: 1, pageSize: 20, seeks: seeks }
        });
        this.loadOptionData();
    }
    loadOptionData() {
        let options = []
        controls.map(c => {
            if (c.fetch) {
                let f = c.fetch;
                if (c.type === 'cascader-select') {
                    f.isCascader = true;
                }
                if (c.type !== 'search') {
                    options.push(f);
                }
            }
        })
        if (daoName && options.length > 0) {
            this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
        }
    }
    showModelHandler(isAdd) {
        let key = null
        if (!isAdd) {
            const len = this.props.selectedRowKeys.length;
            if (len === 0) {
                message.info('请选择要认定的数据行！');
                return;
            }
            key = this.props.selectedRowKeys[len - 1]
        }
        this.props.dispatch({
            type: `${daoName}/openEdit`,
            payload: { key }
        });
    }
    findData() {
        const form = this.refs.findForm.getForm();
        let nyc = '';
        let nym = '';
        let isOK = true;
        form.validateFields((err, values) => {
            if (!err) {
                if (values.YF === undefined || values.YF === '' || values.YF === null) {
                    message.info('请选择年月！');
                    isOK = false;
                } else {
                    let date = new Date(values.YF.get('year'), values.YF.get('month'), 1, 8, 0, 0);
                    let year = date.getFullYear();
                    let month = date.getMonth() + 1;
                    let day = new Date(year, month, 0);
                    if (month < 10) {
                        month = '0' + month;
                    }
                    nym = year + '-' + month + '-' + day.getDate();
                    nyc = datetime.format(date, "yyyy-MM-dd");
                }
            }
        });
        if (!isOK) {
            return;
        }
        let whereItems = [];
        let WhereCauses = [];
        whereItems.push({
            FieldName: "Sjrq",
            Operator: ">=",
            FieldValues: nyc
        }, {
                FieldName: "Sjrq",
                Operator: "<=",
                FieldValues: nym
            });
        WhereCauses.push({
            WhereItems: whereItems,
            RelationSign: "AND"
        })
        let seeks = WhereCauses;
        this.props.dispatch({
            type: `${daoName}/fetch`,
            payload: { daoName, seeks }
        });
    }
    render() {
        let { showEdit, record, showImport, popedomTypes, permID, cmDate } = this.props
        const pTypes = popedomTypes[permID]
        let initYF = cmDate.substr(0, 7)
        let data = [{ value: '1', label: '查询', onClick: this.findData.bind(this) }]
        if (pTypes.indexOf('6') !== -1) {
            let button = { value: '2', label: '业绩认定', onClick: this.showModelHandler.bind(this, false) }
            data.push(button)
        }
        const controlsFilter = [
            { name: 'YF', required: false, type: 'month', label: '年月', grid: 5 / 24, layout: { labelSpan: 8, wrapperSpan: 14 }, dataType: 'month' },
            { name: 'CX', required: false, type: 'button-group', grid: 5 / 24, data: data, grid: 5 / 24 },
        ];
        return (
            <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
                <div>
                    <div className={styles.create}>
                        <EditFrom ref="findForm" daoName={daoName} record={{ YF: initYF }} controls={controlsFilter} clearItemMargin={true} />
                    </div>
                    <INDI_QTZB_DIVI_FHQK ref="divi" jlid={record.ID} ywid={9} title={"【" + record.Khmc + "】 业绩分配"} showEdit={showEdit} parentModel={daoName} />
                    <DataTable ref="userTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
                </div>
            </div>
        )
    }
}
function mapStateToProps(state) {
    const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
    const { userID, orgID, pOrgID, popedomTypes, cmDate } = state.menus;
    return {
        userID,
        orgID,
        pOrgID,
        cmDate,
        popedomTypes,
        showEdit,
        selectedRowKeys,
        keyName,
        record
    }
}
CM_CRDT_CARD_SUM_M = connect(mapStateToProps)(CM_CRDT_CARD_SUM_M)
export default CM_CRDT_CARD_SUM_M;
let render = (props) => React.createElement(CM_CRDT_CARD_SUM_M, props);

let f = new Factory()
f.set("CM_CRDT_CARD_SUM_M", render)


